package com.win.mapreduce;

import com.win.common.HdfsCommon;
import lombok.Data;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Data
public class WordCountDriver {


    public int executeMapReduceJob(String path) throws IOException, InterruptedException, ClassNotFoundException {

        Configuration configuration = new Configuration();
        configuration.set("mapreduce.framework.name","local");
        configuration.set("fs.defaultFS", HdfsCommon.hdfsUri);

        Job job = Job.getInstance(configuration);

        job.setMapperClass(WordCountMap.class);
        job.setReducerClass(WordCountReduce.class);
        job.setPartitionerClass(WordCountPartitioner.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setNumReduceTasks(2);

        FileInputFormat.setInputPaths(job,new Path("/mapreduce/input"));
        FileOutputFormat.setOutputPath(job,new Path(path));

        //提交job
        return job.waitForCompletion(true) ? 0 : -1;
    }


}
